package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.mapper.TrafficMapper;
import com.adcc.ags.gms.gw.vo.TrafficVO;
import com.adcc.ags.gms.user.entity.GWUser;
import com.adcc.ags.gms.user.mapper.GWUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by xuechao on 2016/9/2.
 */
@Service("TrafficService")
public class TrafficServiceImpl implements TrafficService {

    @Autowired
    private TrafficMapper trafficMapper;

    @Autowired
    private GWUserMapper gwUserMapper;

    @Autowired
    private LogFactory log;

    @Override
    public Optional<PageInfo<TrafficVO>> findAll(String rawMsg, String state, String dateStart, String dateEnd, String sender, String receiver, String msgType,Integer currentPage, Integer pageSize) {
        try {
            if(currentPage != null && pageSize != null){
                PageHelper.startPage(currentPage, pageSize);
            }
            if(Strings.isNullOrEmpty(rawMsg)||"undefined".equals(rawMsg)){
                rawMsg=null;
            }
            if(Strings.isNullOrEmpty(dateStart)||"undefined".equals(dateStart)){
                dateStart=null;
            }
            if(Strings.isNullOrEmpty(dateEnd)||"undefined".equals(dateEnd)){
                dateEnd=null;
            }
            List<TrafficVO> list = trafficMapper.findAll(rawMsg, state, dateStart, dateEnd, sender, receiver, msgType);
            // 判断接收方页面传值是否为空
            if (receiver == null || receiver.equals("-1")) {
                for (TrafficVO trafficVO : list) {
                    //发送方页面转译
                    GWUser userConfig1 = gwUserMapper.findGWUserById(Long.parseLong(trafficVO.getSender()));
                    if (userConfig1 == null) {
                        //查不到用户name时，显示id
                        trafficVO.setSenderName(trafficVO.getSender());
                    } else {
                        trafficVO.setSenderName(userConfig1.getUserName());
                    }
                    //接收方多个转译
                    String receiverStr = trafficVO.getReceiver();
                    if (receiverStr == null || receiverStr.equals("")) {
                        trafficVO.setReceiverName("");
                    } else {
                        String[] arry = receiverStr.split(",");
                        String receiverNameStr = "";
                    /* todo 如果根据ID查不到用户，需要做处理 */
                        List<GWUser> userConfigList = gwUserMapper.findGWUserByIdList(arry);
                        if (userConfigList.size() > 0) {
                            for (GWUser userConfig2 : userConfigList) {
                                receiverNameStr = receiverNameStr + "," + userConfig2.getUserName();
                            }
                            trafficVO.setReceiverName(receiverNameStr.substring(1));
                        } else {
                            trafficVO.setReceiverName(receiverStr);
                        }

                    }
                }
                PageInfo<TrafficVO> pageInfo = new PageInfo<>(list);
                return Optional.of(pageInfo);
            } else {
                //页面转译之前需要对接收方筛选
                List<TrafficVO> listNew = new ArrayList<TrafficVO>();
                for (TrafficVO trafficVO : list) {
                    String receiverStrs = trafficVO.getReceiver();
                    String[] arry = receiverStrs.split(",");
                    if (Arrays.asList(arry).contains(receiver)) {
                        listNew.add(trafficVO);
                    }
                }
                //页面转译
                for (TrafficVO trafficVO : listNew) {
                    //发送方页面转译
                    GWUser userConfig1 = gwUserMapper.findGWUserById(Long.parseLong(trafficVO.getSender()));
                    if (userConfig1 == null) {
                        //查不到用户name时，显示id
                        trafficVO.setSenderName(trafficVO.getSender());
                    } else {
                        trafficVO.setSenderName(userConfig1.getUserName());
                    }
                    //接收方多个转译
                    String receiverStr = trafficVO.getReceiver();
                    if (receiverStr == null || receiverStr.equals("")) {
                        trafficVO.setReceiverName("");
                    } else {
                        String[] arry = receiverStr.split(",");
                        String receiverNameStr = "";
                    /* todo 如果根据ID查不到用户，需要做处理 */
                        List<GWUser> userConfigList = gwUserMapper.findGWUserByIdList(arry);
                        if (userConfigList.size() > 0) {
                            for (GWUser userConfig2 : userConfigList) {
                                receiverNameStr = receiverNameStr + "," + userConfig2.getUserName();
                            }
                            trafficVO.setReceiverName(receiverNameStr.substring(1));
                        } else {
                            trafficVO.setReceiverName(receiverStr);
                        }
                    }
                }
                PageInfo<TrafficVO> pageInfo = new PageInfo<>(list);
                return Optional.of(pageInfo);
            }
        } catch (NumberFormatException e) {
            log.error(TrafficService.class, "Traffic", GWLogLevel.ALARM, "findAll() error", e);
            return Optional.absent();
        }
    }

}
